import request from '@utils/request'
import { APIParams, APIResult } from '@utils/request/type'
import {
  AlarmDeviceStatisticsVO,
  AlarmDTO, AlarmGroupStatisticsVO,
  AlarmHistoryDTO,
  AlarmRuleSaveForm,
  AlarmStatisticsVO, AlarmUserDeviceDTO, DeviceRoleAlarmDTO
} from '@/api/entity/alarm'
import { ServerEnum } from '@/api/enum/server'
import { ObjectUtil } from '@utils/object-util'

// region TODO 报警记录
/**
 * 分页查询
 * @param params
 */
export const getAlarmListByPageAPI = (params: APIParams.Page<{
  // 状态 false-正常
  alarmState?: boolean,
  // 推送类型 0-公众号 1-短信 2-播报
  informType?: string,
  // 是否消音
  status?: boolean
  // 设备id
  deviceId?: string
  // 结束时间
  endTime?: string
  // 开始时间
  startTime?: string
  // 变量id
  variableId?: string
}>) => request.post<APIResult.Page<AlarmHistoryDTO[]>>(ServerEnum.System + '/alarmHistory/page', params)

/**
 * 获取需要播报的报警
 * @param params
 */
export const getAlarmMsgListByPageAPI = (params: APIParams.Page<{
  // 结束时间
  endTime?: string
  // 开始时间
  startTime?: string
}>) => {
  const _params = ObjectUtil.copy(params)
  Object.assign(_params.param, {
    // 状态 false-正常
    alarmState: true,
    // 推送类型 0-公众号 1-短信 2-播报
    informType: '2',
    // 是否消音
    status: false
  })
  return request.post<APIResult.Page<AlarmHistoryDTO[]>>(ServerEnum.System + '/alarmHistory/page', _params)
}

/**
 * 报警记录统计 - 页面级
 */
export const getAlarmStatisticsAPI = () => request.get<APIResult<AlarmStatisticsVO>>(ServerEnum.System + '/alarmHistory/statistics')

/**
 * 根据设备按天统计报警数据
 */
export const getStatisticalAlarmDataByDayToDeviceAPI = (params: {
  deviceId: string,
  startTime: string,
  endTime: string
}) => request.post<APIResult<AlarmDeviceStatisticsVO[]>>(ServerEnum.System + '/alarmHistory/statisticsByDevice', params)

/**
 * 消音
 */
export const alarmBecomeSilentAPI = (params: {
  id: string,
  // true-消音
  status: boolean
}) => request.post(ServerEnum.System + '/alarmHistory/update', params)

/**
 * 获取时间最久的一条播报的报警日期
 */
export const getMaxOldTimeAlarm = () => request.get<APIResult<string>>(ServerEnum.System + '/alarmHistory/getOldTime')

/**
 * 根据场景Id统计场景下设备的报警条数（不包含已恢复和消音） - 组态用
 * @param params
 */
export const getAlarmStatisticsBySceneId = (params: {
  groupId: string,
  startTime: string,
  endTime: string
}) => request.post<APIResult<AlarmGroupStatisticsVO[]>>(ServerEnum.System + '/alarmHistory/statisticsByGroup', params)
// endregion

// region TODO 报警规则
/**
 * 分页查询
 * @param params
 */
export const getAlarmRulesListByPageAPI = (params: APIParams.Page<{
  q: string,
  deviceId: string
}>) => request.post<APIResult.Page<AlarmDTO[]>>(ServerEnum.System + '/alarm/page', params)

/**
 * 获取报警规则所在的页数
 * @param params
 */
export const getAlarmRulePageNumByIdAPI = (params: APIParams.Page<{ id: string }>) => request.post(ServerEnum.System + '/alarm/getPageNum', params)

/**
 * 根据id获取报警规则
 * @param params
 */
export const getAlarmRulesByIdAPI = (params: { id: string }) => request.get<APIResult<AlarmDTO>>(ServerEnum.System + '/alarm/getById/' + params.id)

/**
 * 添加报警规则
 * @param params
 */
export const addAlarmRuleAPI = (params: AlarmRuleSaveForm) => request.post(ServerEnum.System + '/alarm/save', params)

/**
 * 编辑报警规则
 * @param params
 */
export const editAlarmRuleAPI = (params: AlarmRuleSaveForm) => request.post(ServerEnum.System + '/alarm/update', params)

/**
 * 删除报警规则
 * @param params
 */
export const delAlarmRuleAPI = (params: { id: string }) => request.get(ServerEnum.System + '/alarm/deleteById/' + params.id)

/**
 * 启用/停用报警规则
 * @param params
 */
export const toggleAlarmEnableAPI = (params: {
  id: string,
  // true-启用
  enable: boolean
}) => request.post(ServerEnum.System + '/alarm/update', params)
// endregion

// region TODO 报警接收配置
/**
 * 分页查询
 * @param params
 */
export const getAlarmRulesConfigListByPageAPI = (params: APIParams.Page<{
  q: string,
  // 4-仅自己 3-与我分享
  type: number
}>) => request.post<APIResult.Page<DeviceRoleAlarmDTO[]>>(ServerEnum.System + '/deviceRole/page2Alarm', params)

/**
 * 编辑报警接收配置
 * @param params
 */
export const editAlarmRulesConfigListByPageAPI = (params: AlarmUserDeviceDTO) => request.post(ServerEnum.System + '/alarmUserDevice/update', params)

// endregion
